﻿Imports System.Web.Configuration
Imports System.Data.SqlClient
Imports System.Data

Partial Class Default2
    Inherits System.Web.UI.Page
    Private connectionString As String = WebConfigurationManager.ConnectionStrings("Database").ConnectionString
    Dim questionsArray As New ArrayList
    Dim answersArray As New ArrayList

    Protected Sub Default2_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsPostBack = False Then
            quizIDHiddenField.Value = CType(PreviousPage.FindControl("quizIDHiddenField"), HiddenField).Value
            userNameHiddenField.Value = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value
            userNameLabel.Text = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value.ToString
            quizTitleLabel.Text = CType(PreviousPage.FindControl("titleHiddenField"), HiddenField).Value.ToString
            quizAuthor.Text = CType(PreviousPage.FindControl("ownerNameHiddenField"), HiddenField).Value.ToString
            fillQuestions()
        End If
    End Sub

    Private Sub fillQuestions()
        Dim myConnection As New SqlConnection(connectionString)
        'questionLayout = getQuestionIDs(quizID)
        Dim selectQuiz As String = "SELECT * FROM QuestionSelect WHERE Quiz_Id = '" & quizIDHiddenField.Value & "'"
        Dim myCommand As New SqlCommand(selectQuiz, myConnection)
        Dim myDataAdapter As New SqlDataAdapter(myCommand)
        Dim questions As New DataSet()

        Try
            myConnection.Open()
            myDataAdapter.Fill(questions, "QuestionSelect")
            For Each row As DataRow In questions.Tables("QuestionSelect").Rows
                Dim newitem As New ListItem
                newitem.Text = row("Question")
                newitem.Value = row("question_Id")
                questionsArray.Add(newitem.Value)
            Next
            fillAnswers()
        Catch ex As Exception
            errorLabel.Text = "Error getting your questions. " & ex.Message
        Finally
            myConnection.Close()
        End Try
    End Sub

    Private Sub fillAnswers()
        For count As Integer = 0 To questionsArray.Count - 1 Step 1
            Dim getIDSQL As New StringBuilder
            getIDSQL.Append("SELECT * FROM QuestionSelect WHERE question_Id = '" & questionsArray.Item(count) & "'")
            Dim myConnection As New SqlConnection(connectionString)
            Dim myCommand As New SqlCommand(getIDSQL.ToString, myConnection)
            Dim myReader As SqlDataReader
            Try
                myConnection.Open()
                myReader = myCommand.ExecuteReader
                myReader.Read()
                resultLabel.Text &= "<b></br>" & myReader("Question") & "</br></br></br></b>"
                ''for loop for answers
                answersArray.Clear()
                questionIDHiddenField.Value = questionsArray.Item(count)
                questionLengthHiddenField.Value = questionsArray.Count
                fillAnswersArray()
                For countAnswers As Integer = 0 To answersArray.Count - 1 Step 1
                    Dim myConnection2 As New SqlConnection(connectionString)
                    Dim getAnswerSQL As String = "SELECT * FROM Answers WHERE Id = '" & answersArray.Item(countAnswers) & "'"
                    Dim myCommand2 As New SqlCommand(getAnswerSQL, myConnection2)
                    Dim myReader2 As SqlDataReader


                    Try
                        myConnection2.Open()
                        myReader2 = myCommand2.ExecuteReader
                        myReader2.Read()
                        Dim radioButton = New RadioButton()
                        radioButton.Text = ""
                        radioButton.GroupName = "Group" & countAnswers
                        radioButton.Visible = True
                        resultLabel.Text &= "__ " & myReader2("Answer") & "</br>"

                    Catch ex As Exception
                        errorLabel.Text = "Cant get answers bruh" & ex.Message
                    Finally
                        myConnection2.Close()
                    End Try
                Next

            Catch ex As Exception
                errorLabel.Text = "Cant get question bruh" & ex.Message
            Finally
                myConnection.Close()
            End Try
        Next

    End Sub

    Private Sub fillAnswersArray()
        Dim myConnection As New SqlConnection(connectionString)
        Dim selectQuiz As String = "SELECT * FROM Answers WHERE questionID = '" & questionIDHiddenField.Value & "'"
        Dim myCommand As New SqlCommand(selectQuiz, myConnection)
        Dim myDataAdapter As New SqlDataAdapter(myCommand)
        Dim answers As New DataSet()

        Try
            myConnection.Open()
            myDataAdapter.Fill(answers, "Answers")
            For Each row As DataRow In answers.Tables("Answers").Rows
                Dim newitem As New ListItem
                newitem.Text = row("Answer")
                newitem.Value = row("Id")
                answersArray.Add(newitem.Value)
            Next
        Catch ex As Exception
            errorLabel.Text = "Error getting your answers. " & ex.Message
        Finally
            myConnection.Close()
        End Try
    End Sub







    'Private Sub fillGridView()
    '    For index As Integer = 1 To questionLengthHiddenField.Value
    '        Dim tcLabel As New TableCell
    '        Dim tcRadioButtonList As New TableCell
    '        Dim questionLabel As Label = New Label
    '        Dim dynamicRbl As New RadioButtonList
    '        Dim tableRowForLabel As New TableRow
    '        Dim tableRowForRbl As New TableRow
    '        randomQuestion = Session("randomQuestion")

    '        'Creates label for the question
    '        Dim labelName As String
    '        labelName = "question" + index.ToString() + "lbl"
    '        questionLabel.ID = labelName
    '        questionLabel.Text = quiz.getQuizQuestions(Convert.ToInt32(randomQuestion.Rows(index - 1)("index")))
    '        questionLabel.Text = ("<br/><br?><b>" & questionLabel.Text & "<?b>")
    '        tcLabel.Controls.Add(questionLabel)

    '        'Creates dynamic radiobuttonlist for each group of answers
    '        Dim radioButtonName As String
    '        radioButtonName = "question" + index.ToString() + "RadioButtonList"
    '        dynamicRbl.ID = radioButtonName

    '        Dim quizAnswers As DataTable = New DataTable()
    '        quizAnswers = quiz.getQuizAnswers(Convert.ToInt32(randomQuestion.Rows(index - 1)("index")))
    '        For k As Integer = 0 To quizAnswers.Rows.Count - 1
    '            dynamicRbl.Items.Add(quizAnswers.Rows(k)(0).ToString())
    '        Next
    '        'dynamicRbl.SelectedIndex = 0
    '        tcRadioButtonList.Controls.Add(dynamicRbl)
    '        tableRowForLabel.Cells.Add(tcLabel)
    '        tblQuiz.Rows.Add(tableRowForLabel)
    '        tableRowForRbl.Cells.Add(tcRadioButtonList)

    '        tblQuiz.Rows.Add(tableRowForRbl)
    '    Next

    Protected Sub submitAnswersButton_Click(sender As Object, e As EventArgs) Handles submitAnswersButton.Click

    End Sub
End Class
